/*
 * Alloy Analyzer
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.util;

import java.util.HashSet;
import java.util.Set;

/**
 * A set of longs.
 */
public class SetOfLong {
	/** For now, this is implemented as a {@link Set} of {@link Long}. */
	private Set /* of Long */_elems;

	/**
	 * constructor
	 */
	public SetOfLong () {
		_elems = new HashSet();
	}

	/**
	 * @return the number of elements in the container
	 */
	public int size() {
		if(_elems == null)
			return 0;
		else
			return _elems.size();
	}

	/**
	 * @return a {@link Set} of objects that is equivalent to the container
	 */
	public Set toSet() {
		if(_elems == null)
			return new HashSet();
		else
			return _elems;
	}

	/**
	 * @param e_ the long to be added
	 * @mutates the container by adding e_
	 */
	public void add(long e_) {
		if (_elems == null)
			_elems = new HashSet();
		_elems.add(new Long(e_));
	}

	/**
	 * @param s a SetOfLong to be added
	 * @mutates the container by adding all elements of s
	 */
	public void addAll(SetOfLong s) {
		if (_elems == null)
			_elems = new HashSet();
		if (s != null && s._elems != null)
			_elems.addAll(s._elems);
	}

	/**
	 * @param e_ the long to be removed if present
	 * @mutates the container to no longer contain e_ (if it ever did)
	 */
	public void remove(long e_) {
		if (_elems != null)
			_elems.remove(new Long(e_));
	}

	/**
	 * @param e_ the long in question
	 * @return whether or not the given long is in the container
	 */
	public boolean contains(long e_) {
		if(_elems == null)
			return false;
		else
			return _elems.contains(new Long(e_));
	}

	/**
	 * @return whether or not the container is empty
	 */
	public boolean isEmpty() {
		if(_elems == null)
			return true;
		else
			return _elems.isEmpty();
	}

	/**
	 * @returns a hashCode for the container
	 */
	public int hashCode() {
		if (_elems == null)
			return 0;
		else
			return _elems.hashCode();
	}

	/**
	 * @returns the result of a contents-based equality test
	 */
	public boolean equals(Object that_) {
		if (!(that_ instanceof SetOfLong))
			return false;
		Set /* of Long */thatElems = ((SetOfLong) that_)._elems;
		return (_elems == null && thatElems == null)
				|| (_elems != null && thatElems != null && _elems
						.equals(thatElems));
	}

	/**
	 * @returns a string representation of the container
	 */
	public String toString() {
		if (_elems == null)
			return "[]";
		else
			return _elems.toString();
	}
}
